﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BHair.Business.Table;
using System.Data.SqlClient;
using System.Configuration;
using BHair.xitong;


namespace BHair
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
       // SoftKeyYT88 ytsoftkey;
        public static string KeyPath;
        public static string gouhao;
        public static string jjmm;
        private void btnLogin_Click(object sender, EventArgs e)
        {
            EmpMoney EmpMoney = new EmpMoney();


            if (EmpMoney.CheckPass(comboBox3.Text, txtPass.Text) == true)
            {
                this.Visible = false;

                // frmMain frmMain = new frmMain();
                // frmMain.Show();
                chuanzhi.userID = comboBox3.Text;
               
                duqubanzu();
              


                //frmzhu zhu = new frmzhu();

                frmzhuchuang zhu = new frmzhuchuang();
                zhu.Show();
                chuanzhi cz = new chuanzhi();
                frmtixing tx = new frmtixing();
                tx.Show();
                cz.rizhi("登陆");

                //判断是否有加密锁

                //MessageBox.Show("读字符串成功：" + outstring.ToString());



            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            SoftKey so = new SoftKey();
            //这个用于判断系统中是否存在着加密锁。不需要是指定的加密锁,
            if (so.FindPort(0, ref KeyPath) != 0)
            {
                MessageBox.Show("未找到加密锁,请插入加密锁后，再进行操作。");
                Application.Exit();
            }
            try
            {
                jiazaiuser();

                rsdjh();
            }
            catch
            {
                frmpz pz = new frmpz();
                pz.ShowDialog();
            }
            
        }
        //判断是否过了凌晨23点
        //如果是第二天那么重置单据编号
        public void rsdjh()
        {
            //当前日期
            string dqdate = DateTime.Now.ToString("yyyy-MM-dd");
            //获取数据库时间
            string dsdate ="";
            SqlConnection conn1 = new SqlConnection(constr);
            conn1.Open();
            string sql1 = "select 单据日期 from bb";
            SqlCommand comm1 = new SqlCommand(sql1,conn1);
            SqlDataReader sdr = comm1.ExecuteReader();
            while (sdr.Read())
            {
                dsdate = sdr["单据日期"].ToString();
            }
            conn1.Close();
            //比较日期大小
            if (DateTime.Parse( DateTime.Now.ToString("yyyy-MM-dd")) > DateTime.Parse(dsdate))
            {
                //重置所有单据号
                SqlConnection conn2 = new SqlConnection(constr);
                conn2.Open();
                string sql2 = "UPDATE [djh] SET [lsdjh] = '000000' ,[jhdjh] = '000000' ,[czdjh] = '000000' ,[jcxf] = '000000' ,[xuqi] = '000000' ,[hycz] = '000000' ,[jccz] = '000000' ,[bk] = '000000' ,[qtck] = '000000'";
                SqlCommand comm2 = new SqlCommand(sql2,conn2);
                comm2.ExecuteNonQuery();
                conn2.Close();
                //修改系统数据库时间为当前时间日期
                SqlConnection conn3 = new SqlConnection(constr);
                conn3.Open();
                string sql3 =string.Format( "UPDATE [bb] SET [单据日期] ='{0}'",DateTime.Now.ToString("yyyy-MM-dd"));
                SqlCommand comm3 = new SqlCommand(sql3,conn3);
                comm3.ExecuteNonQuery();
                conn3.Close();
            }
            else
            {

            }
        }

          private void btnExit_Click(object sender, EventArgs e)
          {
              Application.Exit();
          }

          private void button1_Click(object sender, EventArgs e)
          {
              frmpz pz = new frmpz();
              pz.ShowDialog();
          }

          private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
          {
              frmpz pz = new frmpz();
              pz.ShowDialog();
          }

          private void button2_Click(object sender, EventArgs e)
          {

              Application.Exit();
          }
          public void duqubanzu()
          {
              SqlConnection conn = new SqlConnection(constr);
              try
              {
                  conn.Open();
                  string sql =string.Format( "SELECT [班别] FROM [BHairDB].[dbo].[LoginUser] where LoginUserID='{0}'",comboBox3.Text);
                  SqlCommand comm = new SqlCommand(sql,conn);
                  SqlDataReader sdr = comm.ExecuteReader();
                  while (sdr.Read())
                  {
                      chuanzhi.banzu = sdr["班别"].ToString();
                  }
              }
              catch (Exception ex) { MessageBox.Show(ex.Message); }
          }
        public void duqujiam()
        {
            //int ret;
            //short nlen;
            //byte[] buf = new byte[1];
            //string outstring = "";
            //short addr = 0;//要读取的地址
            ////先从地址0读到以前写入的字符串的长度
            //ret = ytsoftkey.YReadEx(buf, addr, 1, "85F30C19", "496552FC", KeyPath);
            //nlen = buf[0];
            //if (ret != 0)
            //{
            //    MessageBox.Show("读取字符串长度错误。错误码：" + ret.ToString()); return;
            //}
            ////再读取相应长度的字符串
            //ret = ytsoftkey.YReadString(ref outstring, addr + 1, nlen, "85F30C19", "496552FC", KeyPath);
            //if (ret != 0)
            //    MessageBox.Show("读取字符串错误。错误码：" + ret.ToString());
            //else
            //   jjmm= outstring.ToString();
        }
        public void shezhijiamisuanfa()
        {
            //int ret;
            //string Key;
            //Key = "67880ED76484805440BDC27313B8B28D";
            //ret = ytsoftkey.SetCal_2(Key, KeyPath);
            //if (ret != 0) { MessageBox.Show("设置增强算法密钥错误"); return; }
            ////MessageBox.Show("已成功设置了增强算法密钥");
        }
       
        
       
        public void jiazaiuser()
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(constr);
            //加载登陆用户
            conn.Open();
            string sql = "SELECT [LoginUserID],[username]  FROM [LoginUser]";
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            sda.Fill(ds, "LoginUser");
            comboBox3.DataSource = ds.Tables["LoginUser"];
            comboBox3.DisplayMember = "LoginUserID";
            conn.Close();
        }

        private void txtPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                EmpMoney EmpMoney = new EmpMoney();


                if (EmpMoney.CheckPass(comboBox3.Text, txtPass.Text) == true)
                {
                    this.Visible = false;

                    // frmMain frmMain = new frmMain();
                    // frmMain.Show();
                    chuanzhi.userID = comboBox3.Text;

                    duqubanzu();



                    //frmzhu zhu = new frmzhu();

                    frmzhuchuang zhu = new frmzhuchuang();
                    zhu.Show();
                    chuanzhi cz = new chuanzhi();
                    frmtixing tx = new frmtixing();
                    tx.Show();
                    cz.rizhi("登陆");

                    //判断是否有加密锁

                    //MessageBox.Show("读字符串成功：" + outstring.ToString());



                }
                else
                {
                    MessageBox.Show("密码错误！");
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            SoftKey so = new SoftKey();
            string OutPath="";

            //'使用增强算法二对字符串进行加密
            short ver = 0;
            if (so.NT_GetVersionEx(ref ver, KeyPath) != 0) { MessageBox.Show("返回加密锁扩展版本号错误"); return; }
            if (ver < 10) { MessageBox.Show("锁的扩展版本少于10,不支持增强算法二"); return; }
            int ret, nlen;
            string InString;
            string outstring = "";
            InString = "123456";
            nlen = SoftKey.lstrlenA(InString) + 1;
            if (nlen < 8) nlen = 8;
            ret = so.EncString_New(InString, ref outstring, KeyPath);
            if (ret != 0) { MessageBox.Show("加密字符串出现错误"); return; }
            MessageBox.Show("已成功对字符串进行加密，加密后的字符串为：" + outstring.ToString());
        }
    }
  }
